<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传头像</title>
    <script src="https://www.jq22.com/jquery/jquery-3.3.1.js"></script>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <button type="submit">上传头像</button>
</form>
<!--< img id="avatar" src="" alt="头像">-->
<script>
    $(document).ajaxError(function(){
<!--       alert("当前用户未登录，请先登录！");-->
       window.location.href="login.html";
    });

var user_token = localStorage.getItem("user_token");
console.log(user_token);
        $("#uploadForm").on("submit", function(e) {
            e.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                url: "/user/upload",
                type: "POST",
                data: formData,
                headers: {
                   'user_token_header': user_token
                },
                processData: false,
                contentType: false,
                success: function(response) {
                       if(response == true) {
                           alert("上传成功");
                           window.location.href="index.html";

                       }else {
                            alert("上传失败");
                            window.location.href="index.html";
                       }
                },
                error: function() {
                    alert("上传失败");
                }
            });
        });
    </script>
</body>
</html>